//
//  EmotionPackage.swift
//  Emotion
//
//  Created by maker on 2016/11/21.
//  Copyright © 2016年 maker. All rights reserved.
//

import UIKit

class EmotionPackage: NSObject {
    var emotions : [EmotionModel] = [EmotionModel]()
    
    init(id : String) {
        super.init()
        
        if id == "" {
            addEmptyAndDel(0)
            return
        }
        
        //1.根据 id 找到 plist 文件
        let plistPath = Bundle.main.path(forResource: "\(id)/info.plist", ofType: nil, inDirectory: "Emoticons.bundle")!
        //2.创建数组
        let arr = NSArray(contentsOfFile: plistPath)! as! [[String:String]]
        
        //3.遍历数组
        var index = 0
        for var dict in arr {
            if let png = dict["png"]  {
                dict["png"] = id + "/" + png
            }
            index += 1
            emotions.append(EmotionModel(dict: dict as [String : AnyObject]))
            if index == 20 {
                emotions.append(EmotionModel(isDelete: true))
                index = 0
            }
        }
        
        //4取21余数为0 正好,不为0就添加空白表情
        let remainder = emotions.count % 21
        
        if remainder == 0 {
            return
        }else{
            addEmptyAndDel(remainder)
        }
        
    }
    
    //添加空白表情和删除表情
    func addEmptyAndDel(_ start: Int) {
        for _ in start..<20 {
            emotions.append(EmotionModel(isEmpty: true))
        }
        emotions.append(EmotionModel(isDelete: true))
    }
    
}
